<?php

class Admin_IndexController extends Zend_Controller_Action
{
    /**
     * @return void
     */
    public function preDispatch()
    {
        $user = JFactory::getUser();
        $actionName = $this->_request->getActionName();
        switch ($actionName) {
            case 'login':
                if (!$user->IsGuest) {
                    $this->redirect('/admin/index/navigation');
                }
                break;
            case 'navigation':
                if ($user->IsGuest) {
                    $this->redirect('/admin/index/login');
                }
                break;
            case 'index':
                $this->redirect('/admin/index/login');
                break;
        }
    }

    /**
     * @return void
     */
    public function init()
    {
        $layout = Zend_Layout::getMvcInstance();
        $actionName = $this->_request->getActionName();
        switch ($actionName) {
            case 'login':
                $layout->setLayout('/admin/layout-index-login');
                break;
            case 'navigation':
                $layout->setLayout('/admin/layout-index-navigation');
                break;
        }
    }

    /**
     * @return void
     */
    public function loginAction()
    {
        $isPost = $this->_request->getParam('isPost');
        if (isset($isPost)) {
            $username = $this->_request->getParam('username');
            $password = $this->_request->getParam('password');

            $authentication = JAuthentication::getInstance();
            $authentication->setRequiredInfo('Username', 'Password');
            $result = $authentication->authenticate($username, $password);
            switch ($result->getCode()) {
                case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
                    $this->view->messages = array('Username Is Not Found');
                    break;
                case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
                    $this->view->messages = array('Password Is Invalid');
                    break;
                case Zend_Auth_Result::SUCCESS:
                    $userID = Application_Model_User::getUserID($username);
                    $user = JFactory::getUser($userID);
                    if (!$user->authorise('backend.login')) {
                        $this->view->messages = array('You Have No Permission');
                    } else {
                        JSession::setUser($user);
                        $this->redirect('/admin/index/navigation');
                    }
                    break;
            }

            return;
        }

        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->messages = $flashMessenger->getMessages();
    }

    /**
     * @return void
     */
    public function navigationAction()
    {
        $userPOJOs = Application_Model_User::getLastLoggedInUsers();
        $this->view->userPOJOs = $userPOJOs;

        $productPOJOs = Application_Model_Product::getLastCreatedProducts();
        $this->view->productPOJOs = $productPOJOs;
    }

    /**
     * @return void
     */
    public function logoutAction()
    {
        $user = JFactory::getUser();
        $user->updateLastLogout();
        JSession::setUser(null);
        $this->redirect('/admin/index/login');
    }
}





